%%% This is an Euler equation system for solving k and H in each time t.

function val = FOC_fossil_1(x)

global parms i Sf kf Nf cf PRf gf gdNf gdSf Q
% Note that parms is a 1x14 vector with elements:
%
% parms = [delta A Sbar alpha0 alpha1 alpha2 alpha3 Gamma1 alpha psi beta gamma Q0 popgr];
%           1    2   3      4      5    6     7       8      9    10  11    12  13  14  

delta = parms(1);
A = parms(2);
Sbar = parms(3);
alpha0 = parms(4);
alpha1 = parms(5);
alpha2 = parms(6);
alpha3 = parms(7);
beta = parms(11);

k = x(1);
N = x(2);


S = Sf(i-1)-Q(i)*A*k;

g =  alpha0 + alpha1./(Sbar-alpha2./(alpha3+N)-S);
gdN = -alpha1.*alpha2./((Sbar-S).*(alpha3+N)-alpha2).^2;
gdS = alpha1*(alpha3+N).^2./((Sbar-S).*(alpha3+N)-alpha2).^2;



val(1) = A-gf(i-1)+1-delta+Q(i-1)*A*((1-A*kf(i-1)*gdNf(i-1))* ...
    (g-k*gdN-1+delta/A)/Q(i)+A*kf(i-1)*gdSf(i-1))-PRf(i);
val(2) = A*k*(1-g)+(1-delta)*k+N-cf(i)-kf(i-1)-Nf(i-1);





